﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class AddNew : Form
    {
        MainScreen mainScreen = null;
        danquanEntities danquanInfoModel = null;
        dqinfo chosenPerson = null;

        public AddNew()
        {
            InitializeComponent();
        }

        public AddNew(MainScreen mainScreen, danquanEntities danquanInfoModel, dqinfo chosenPerson)
        {
            InitializeComponent();
            this.mainScreen = mainScreen;
            this.chosenPerson = chosenPerson;
            this.danquanInfoModel = danquanInfoModel;
        }

        public AddNew(MainScreen mainScreen)
        {
            InitializeComponent();
            this.mainScreen = mainScreen;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                // Validation
                validationFields();

                if (MessageBox.Show("Bạn muốn thêm thông tin này vào hệ thống?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    /**
                     * Attach attributes to model
                     * */
                    chosenPerson.tenThuongDung = txtTenThuongDung.Text;
                    chosenPerson.tenKhaiSinh = txtTenKhaiSinh.Text;
                    chosenPerson.DOB = dtpDob.Value;
                    chosenPerson.CMND = int.Parse(txtCMND.Text);
                    chosenPerson.noiSinh = txtNoiSinh.Text;

                    // save model to database
                    danquanInfoModel.AddTodqinfoes(chosenPerson);
                    try
                    {
                        danquanInfoModel.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        DialogResult result = MessageBox.Show("Đã thêm thông tin thành công!\n - Chọn \"OK\" để quay lại Màn hình chính.", "Thông Báo!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            btnBack_Click(sender, e);
                        }
                    }

                    // if insert successful, return back to mainscreen
                    this.Dispose();
                    this.Close();
                    this.mainScreen.Show();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Có lỗi!");
            }
        }

        #region Validate fields
        // Validate compulsary fields
        private void validationFields()
        {
            if (txtTenKhaiSinh.Text.Equals(""))
            {
                throw new Exception("Tên khai sinh không được để trống!");
            }

        } 
        #endregion

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
            this.mainScreen.Show();
        }

        private void AddNew_Load(object sender, EventArgs e)
        {
            if (danquanInfoModel != null)
            {
                this.btnSave.Text = "BỔ SUNG";
            }

            // pre-set information
            if (chosenPerson == null)
            {
                chosenPerson = new dqinfo();
            }
            else
            {
                txtTenThuongDung.Text = chosenPerson.tenThuongDung;
                txtTenKhaiSinh.Text = chosenPerson.tenKhaiSinh;
                dtpDob.Value = chosenPerson.DOB;
                txtCMND.Text = chosenPerson.CMND.ToString();
                txtNoiSinh.Text = chosenPerson.noiSinh;

            }
        }
    }
}
